package com.dx.financehy.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * 返回数据
 *
 * @param <T>
 */
@ApiModel(value = "响应参数")
@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "错误码：0-成功，1-失败")
    private int code;

    @ApiModelProperty(value = "错误信息")
    private String msg;

    @ApiModelProperty(value = "业务数据")
    private T data;

    @ApiModelProperty(value = "总记录数")
    private long total = 0;

    @ApiModelProperty(value = "日志链路追踪id")
    private String logTraceId;

    public String getLogTraceId(){
        return MDC.get(AdminConstant.LOG_TRACE_ID);
    }

    public R() {
        this.code = 0;
        this.msg = "操作成功";
    }

    public static R error() {
        return error(1, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(1, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static R error(int code, Object data, String msg) {
        R r = new R();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static R ok(int code, Object data, String msg) {
        R r = new R();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static R ok(Object data, String msg) {
        R r = new R();
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static R ok(Object object, long total) {
        R r = new R();
        r.setData(object);
        r.setTotal(total);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.setMsg(msg);
        return r;
    }

    public static R ok(Object data) {
        R r = new R();
        r.setData(data);
        return r;
    }

    public static R okStr(Object data) {
        R r = new R();
        r.setData(data);
        return r;
    }

    public static R ok() {
        return new R();
    }

}
